/**
 * Created by dmrj on 2016/4/27.
 */
define(['jquery', 'jeditable/jquery.jeditable', 'model/alarm', 'appConst', 'model/global', 'appMessageBox'], function ($, edit, alarm, appConst, global, msgBox) {
    var MAX_LENGTH = 100;
    return {
        init: function () {
            this.initSysCPWRTable();
            this.initSysCCWRTable();
        },
        initSysCPWRTable: function () {
            alarm.search("alarmRule/findCPWR", function (cp_dataFill) {
                $('#dt_cpw').DataTable({
                    "sDom": "<'row'<'col-sm-12' rt>> <'row'<'col-sm-5' i><'col-sm-7' p>>",
                    "processing": true,
                    "paging": false,
                    "searching": false,
                    "autoWidth": false,
                    "language": appConst.table_language(),
                    "ordering": false,
                    "data": cp_dataFill,
                    "columns": [
                        {"data": "id"},
                        {"data": "indiceName"},
                        {"data": "excellentBoundaryValue"},
                        {"data": "goodBoundaryValue"},
                        {"data": "acceptableBoundaryValue"},
                        {"data": "rectificationBoundaryValue"}
                    ],
                    "columnDefs": [
                        {
                            "targets": [0],
                            "visible": false
                        },
                        {
                            "targets": [1],
                            "render": function (data, type, full) {
                                if (data == 'CA') {
                                    return data + " (%)"
                                }
                                return data;
                            }
                        },
                        {
                            "targets": [2],
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'excellentBoundaryValue').attr('dataId', rowData.id);
                            }
                        }, {
                            "targets": [3],
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'goodBoundaryValue').attr('dataId', rowData.id);
                            }
                        }, {
                            "targets": [4],
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'acceptableBoundaryValue').attr('dataId', rowData.id);
                            }
                        }, {
                            "targets": [5],
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'rectificationBoundaryValue').attr('dataId', rowData.id);
                            }
                        }],
                    "drawCallback": function (settings) {
                        var api = this.api();
                        $('#dt_cpw tbody td.inputTd').jeditable(function (value, settings) {
                            var cell = api.cell(this);
                            cell.data(value);
                            var cpw = document.getElementById('dt_cpw');
                            var id = $(this).attr('dataId');
                            var name = $(this).attr('type');
                            var value = value;

                            var tel = /^[a-zA-Z0-9.]+$/;
                            if (isNaN(value) == true || !tel.test(value)) {
                                value = "";
                            }

                            if (value.length > MAX_LENGTH) {
                                $(this).addClass("tableDataError");
                                settings.placeholder = $.i18n.prop("MESSAGE_VALIDATE_LENGTH", $.i18n.prop("GLOBAL_FIELD_MAX"));
                                return false;
                            } else {
                                $(this).removeClass("tableDataError");
                            }

                            if (value == "") {
                                $(this).addClass("tableDataError");
                                settings.placeholder = $.i18n.prop("VALIDATE_CCW");
                                return false;
                            } else {
                                $(this).removeClass("tableDataError");
                            }
                            var obj = {
                                id: id
                            };
                            if (name == 'excellentBoundaryValue') {
                                var value1 = cpw.rows[id - 9].cells[2].innerHTML;
                                if ((id - 9) == 1 && parseFloat(value) > parseFloat(value1)) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_NEXT");
                                    return false;
                                } else {
                                    $(this).removeClass("tableDataError");
                                }
                                if ((id - 9) != 1 && parseFloat(value) < parseFloat(value1)) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_NEXT");
                                    return false;
                                } else {
                                    $(this).removeClass("tableDataError");
                                }
                                obj.attributeName = 'excellentBoundaryValue';
                                obj.attributeValue = value;
                            }

                            if (name == 'goodBoundaryValue') {
                                var value1 = cpw.rows[id - 9].cells[3].innerHTML;
                                var value2 = cpw.rows[id - 9].cells[1].innerHTML;
                                if ((id - 9) == 1 && (parseFloat(value) > parseFloat(value1))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_NEXT");
                                    return false;
                                } else if ((id - 9) == 1 && (parseFloat(value) < parseFloat(value2))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_PREVIOUS");
                                    return false;
                                }
                                else {
                                    $(this).removeClass("tableDataError");
                                }
                                if ((id - 9) != 1 && (parseFloat(value) < parseFloat(value1))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_NEXT");
                                    return false;
                                } else if ((id - 9) != 1 && (parseFloat(value) > parseFloat(value2))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_PREVIOUS");
                                    return false;
                                }
                                else {
                                    $(this).removeClass("tableDataError");
                                }
                                obj.attributeName = 'goodBoundaryValue';
                                obj.attributeValue = value;
                            }

                            if (name == 'acceptableBoundaryValue') {
                                var value1 = cpw.rows[id - 9].cells[4].innerHTML;
                                var value2 = cpw.rows[id - 9].cells[2].innerHTML;
                                if ((id - 9) == 1 && (parseFloat(value) > parseFloat(value1))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_NEXT");
                                    return false;
                                } else if ((id - 9) == 1 && (parseFloat(value) < parseFloat(value2))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_PREVIOUS");
                                    return false;
                                }
                                else {
                                    $(this).removeClass("tableDataError");
                                }
                                if ((id - 9) != 1 && (parseFloat(value) < parseFloat(value1))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_NEXT");
                                    return false;
                                } else if ((id - 9) != 1 && (parseFloat(value) > parseFloat(value2))) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_PREVIOUS");
                                    return false;
                                }
                                else {
                                    $(this).removeClass("tableDataError");
                                }
                                obj.attributeName = 'acceptableBoundaryValue';
                                obj.attributeValue = value;
                            }

                            if (name == 'rectificationBoundaryValue') {
                                var value1 = cpw.rows[id - 9].cells[3].innerHTML;
                                if ((id - 9) == 1 && parseFloat(value) < parseFloat(value1)) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_GTE_PREVIOUS");
                                    return false;
                                } else {
                                    $(this).removeClass("tableDataError");
                                }
                                if ((id - 9) != 1 && parseFloat(value) > parseFloat(value1)) {
                                    $(this).addClass("tableDataError");
                                    settings.placeholder = $.i18n.prop("VALIDATE_CCW_LTE_PREVIOUS");
                                    return false;
                                } else {
                                    $(this).removeClass("tableDataError");
                                }
                                obj.attributeName = 'rectificationBoundaryValue';
                                obj.attributeValue = value;
                            }

                            var array = new Array();
                            array.push(obj);
                            alarm.update("alarmRule/updateAlarmRuleCPWR", array);
                        }, {
                            height: '20',
                            width: '200',
                            cssclass: 'smart-form',
                            placeholder: $.i18n.prop("VALIDATE_CCW")
                        });
                    }
                });
            });
        },
        initSysCCWRTable: function () {
            alarm.search("alarmRule/findCCWR", function (cc_dataFill) {
                $('#dt_ccw').DataTable({
                    "sDom": "<'row'<'col-sm-12' rt>> <'row'<'col-sm-5' i><'col-sm-7' p>>",
                    "processing": true,
                    "paging": false,
                    "searching": false,
                    "autoWidth": false,
                    "language": appConst.table_language(),
                    "ordering": false,
                    "data": cc_dataFill,
                    "columns": [
                        {"data": "id"},
                        {"data": "code"},
                        {"data": "isVisible"},
                        {"data": "detailRule"},
                        {"data": "points"},
                        {"data": "somePoints"},
                        {"data": "sigma"}
                    ],
                    "columnDefs": [
                        {
                            "targets": [0],
                            "visible": false
                        },
                        {
                            "targets": [2],
                            "render": function (data, type, full) {
                                var result = "";
                                if (data == true) {
                                    result = "YES";
                                } else if (data == false) {
                                    result = "NO";
                                }
                                return result;
                            },
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('selctTd').attr('data-code', 'td_isVisible_' + rowData.id);
                            }
                        }, {
                            "targets": [4],
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'points').attr('dataId', rowData.id);
                            }
                        }, {
                            "targets": [5],
                            "render": function (data, type, full) {
                                var result = "";
                                if (full.code == "R5" || full.code == "R6") {
                                    result = data;
                                } else {
                                    result = "--";
                                }
                                return result;
                            },
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'somePoints').attr('dataId', rowData.id);
                                if (row != 4 && row != 5) {
                                    $(td).removeClass('inputTd');
                                    $(td).attr('style', 'color:#C0C0C0');
                                }
                            }
                        }, {
                            "targets": [6],
                            "render": function (data, type, full) {
                                var result = "";
                                if (full.code == "R2" || full.code == "R3" || full.code == "R4") {
                                    result = "--";
                                } else {
                                    result = data;
                                }
                                return result;
                            },
                            "createdCell": function (td, cellData, rowData, row, col) {
                                $(td).addClass('inputTd').attr('type', 'sigma').attr('dataId', rowData.id);
                                if (row > 0 && row < 4) {
                                    $(td).removeClass('inputTd');
                                    $(td).attr('style', 'color:#C0C0C0');
                                }
                            }
                        }],
                    "drawCallback": function (settings) {
                        var api = this.api();
                        $('#dt_ccw tbody td.inputTd').jeditable(function (value, settings) {
                            var cell = api.cell(this);
                            cell.data(value);
                            var id = $(this).attr('dataId');
                            var name = $(this).attr('type');
                            var value = value;

                            var tel = /^[a-zA-Z0-9.]+$/;
                            if (isNaN(value) == true || !tel.test(value)) {
                                value = "";
                            }

                            if (value.length > MAX_LENGTH) {
                                $(this).addClass("tableDataError");
                                settings.placeholder = $.i18n.prop("MESSAGE_VALIDATE_LENGTH", $.i18n.prop("GLOBAL_FIELD_MAX"));
                                return false;
                            } else {
                                $(this).removeClass("tableDataError");
                            }

                            if (value == "") {
                                $(this).addClass("tableDataError");
                                return false;
                            } else {
                                $(this).removeClass("tableDataError");
                            }
                            var obj = {
                                id: id
                            };

                            if (name == 'detailRule') {
                                obj.attributeName = 'detailRule';
                                obj.attributeValue = value;
                            }

                            if (name == 'points') {
                                obj.attributeName = 'points';
                                obj.attributeValue = value;
                            }

                            if (name == 'somePoints') {
                                obj.attributeName = 'somePoints';
                                obj.attributeValue = value;
                            }

                            if (name == 'sigma') {
                                obj.attributeName = 'sigma';
                                obj.attributeValue = value;
                            }

                            var array = new Array();
                            array.push(obj);
                            alarm.update("alarmRule/updateAlarmRuleCCWR", array);
                        }, {
                            height: '20',
                            width: '100',
                            cssclass: 'smart-form',
                            placeholder: $.i18n.prop("VALIDATE_CCW")
                        });
                        $('#dt_ccw tbody td.selctTd').jeditable(function (value, settings) {
                                var cell = api.cell(this);
                                cell.data(value);
                                $(this).html(cell.data());

                                var code;
                                if (cell.data() == 'NO') {
                                    code = $(this).attr('data-code') + '_' + false;
                                }
                                else {
                                    code = $(this).attr('data-code') + '_' + true;
                                }

                                $(this).attr('data-code', code);
                                var arr = $(this).attr('data-code').split('_');
                                var id = arr[2];
                                var name = arr[1];
                                var value = arr[3];

                                var obj = {
                                    id: id
                                };

                                if (name == 'isVisible') {
                                    obj.attributeName = 'isVisible';
                                    obj.attributeValue = value;
                                }

                                var array = new Array();
                                array.push(obj);
                                alarm.update("alarmRule/updateAlarmRuleCCWR", array);

                            }, {
                                type: 'select',
                                submit: $.i18n.prop('GLOBAL_BUTTON_OK'),
                                data: {"YES": "YES", "NO": "NO"}
                            }
                        );
                    }
                });
            });
        }
    }
});